﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using SendMail;

namespace FormSendMail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnUplEmail_Click(object sender, EventArgs e)
        {
            openFileEmail.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            using (StreamReader sr = new StreamReader(openFileEmail.FileName))
            {
                textEmail.Text = sr.ReadToEnd();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strCheck;
            strCheck = textSMTP.Text;
            if (string.IsNullOrWhiteSpace(strCheck))
            {
                MessageBox.Show("Inserire dati smtp");
                return;
            }
            strCheck = textUsername.Text;
            if (string.IsNullOrWhiteSpace(strCheck))
            {
                MessageBox.Show("Inserire dati Username");
                return;
            }

            strCheck = textPassword.Text;
            if (string.IsNullOrWhiteSpace(strCheck))
            {
                MessageBox.Show("Inserire dati Password");
                return;
            }
            


            MailUtility mail = new MailUtility();
            mail.SmtpHost = textSMTP.Text;
            mail.SmtpUsername = textUsername.Text;
            mail.SmtpPassword = textPassword.Text;
            mail.From = textUsername.Text;
            mail.To.Add("arri.luca@gmail.com");
            mail.EnableSSL = checkSSL.Checked;
            mail.Port = int.Parse(textPort.Text);
            mail.Subject = textSubject.Text;
            ListBox.SelectedObjectCollection attachments = checkedListAttachments.SelectedItems;

            foreach (string att in attachments)
            {
                mail.Attachments.Add(att);
            }

            string strCC = textEmail.Text;
            List<string> liststrCC = new List<string>();
            string[] splitChar=new string[] {";\r\n",";","\n",";\n"};
            liststrCC = strCC.Split(splitChar,StringSplitOptions.RemoveEmptyEntries).ToList<string>();

            foreach (string dest in liststrCC)
                mail.BCC.Add(dest);

            mail.Body = textBody.Text;
            try
            {
                mail.SendMail();
                MessageBox.Show("Mail inviate");
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnAttachments_Click(object sender, EventArgs e)
        {
            openFileAttachments.ShowDialog();
            string name = openFileAttachments.FileName;
            checkedListAttachments.Items.Add(name);
        }

        
    }
}
